package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.system.controller.query.SysLogOperateQuery;
import com.morningstar.system.dao.entity.SysLogOperate;
import com.morningstar.system.service.SysLogOperateService;
import com.morningstar.system.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.morningstar.common.constants.permission.PermissionConstants.DETAIL;
import static com.morningstar.common.constants.permission.PermissionConstants.LIST;

/**
 * 操作日志接口
 *
 * @author Admin
 */
@Slf4j
@RestController
@RequestMapping("/logger/operate")
public class SysLogOperateController {

    private static final String PERMISSION = "logger.operate";

    private static final String PERMISSION_LIST = PERMISSION + LIST;

    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;

    @Resource
    private SysLogOperateService logOperateService;

    @Resource
    private SysUserService userService;

    @PostMapping("/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysLogOperate>> list(@RequestBody CommonQuery<SysLogOperate, SysLogOperateQuery> commonQuery) {
        SysLogOperateQuery query = commonQuery.getQuery();
        LambdaQueryWrapper<SysLogOperate> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getTitle()), SysLogOperate::getTitle, query.getTitle());
            queryWrapper.like(StringUtils.isNotBlank(query.getOperateUrl()), SysLogOperate::getOperateUrl, query.getOperateUrl());
            if (CollUtil.isNotEmpty(query.getOperateTime()) && query.getOperateTime().size() == 2) {
                queryWrapper.between(SysLogOperate::getOperateTime, query.getOperateTime().get(0), query.getOperateTime().get(1));
            }
        }
        queryWrapper.orderByDesc(SysLogOperate::getOperateTime);
        queryWrapper.orderByDesc(SysLogOperate::getLogId);
        Map<Long, String> userIdAndName = userService.selectUserIdAndName();
        IPage<SysLogOperate> page = logOperateService.page(commonQuery.getPage(), queryWrapper);
        page.getRecords().forEach(log -> {
            log.setOperateName(userIdAndName.get(log.getOperateId()));
        });
        return Result.successData(page);
    }

    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysLogOperate> detail(@RequestParam("logId") Long logId) throws Exception {
        SysLogOperate logOperate = logOperateService.getById(logId);
        if (logOperate.getOperateId() != null) {
            logOperate.setOperateName(userService.getById(logOperate.getOperateId().toString()).getUserName());
        }
        return Result.successData(logOperate);
    }
}
